<template>
  <div>
    <!-- title的公共组件 -->
    <con-title :topTitle="topTitle"></con-title>

    <el-tabs
      type="border-card"
      class="tab-con"
      v-model="query.agreement"
      @tab-click="changeTabFun"
    >
      <el-tab-pane label="所有设备" name="all"></el-tab-pane>
      <el-tab-pane label="LoRaWAN" name="LoRaWAN"></el-tab-pane>
      <el-tab-pane label="LoRaPP" name="LoRaPP"></el-tab-pane>
      <div>
        <!-- 表单 -->
        <el-form label-width="100px">
          <!-- 下拉菜单 -->
          <el-form-item label="在线状态：">
            <el-select
              v-model="query.online"
              placeholder="请选择"
              class="input-width"
            >
              <el-option
                v-for="item in options"
                :label="item.label"
                :value="item.value"
                :key="item.value"
              ></el-option>
            </el-select>
          </el-form-item>

          <!-- eui搜索 -->
          <el-form-item label="EUI:">
            <el-input v-model.trim="query.eui" class="input-width"></el-input>
          </el-form-item>

          <!-- 绑定时间  新添加的内容 -->
          <el-form-item label="绑定时间:">
            <el-date-picker
              v-model="query.date"
              type="daterange"
              unlink-panels
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            >
            </el-date-picker>
          </el-form-item>
          <!-- 按钮组 -->
          <el-form-item
            ><el-button-group size="mini">
              <el-button
                type="primary"
                size="mini"
                icon="el-icon-search"
                @click="searchFun"
                >搜索</el-button
              >
              <el-button
                type="primary"
                size="mini"
                icon="el-icon-delete"
                @click="clearFun"
                >清除条件</el-button
              >
              <el-button
                type="primary"
                size="mini"
                icon="el-icon-refresh"
                @click="refreshFun"
                >刷新</el-button
              >
            </el-button-group>
            <span class="result">搜索结果：{{ total }}</span>
          </el-form-item>
        </el-form>
      </div>
    </el-tabs>
    <!-- 使用表格显示网关数据 -->
    <el-card class="box-card">
      <el-table :data="gatewayList" style="width: 100%" class="gateway-table">
        <el-table-column type="index" label="序号" width="100">
        </el-table-column>

        <el-table-column prop="eui" label="EUI" width="180"> </el-table-column>
        <el-table-column prop="name" label="设备名称" width="180">
        </el-table-column>
        <el-table-column prop="mhz" label="频率MHz"> </el-table-column>
        <el-table-column label="在线状态">
          <template slot-scope="scope">
            {{ scope.row.online | onlineType }}
          </template>
        </el-table-column>
        <el-table-column prop="country" label="国家"> </el-table-column>
        <el-table-column prop="agreement" label="组网协议类型">
        </el-table-column>
        <el-table-column label="绑定时间">
          <template slot-scope="scope">
            {{ scope.row.bTime | Btime }}
          </template>
        </el-table-column>
      </el-table>
      <div class="goPage">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="query.currPage"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="query.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
// 1.引入api
import { $gatewayList } from "@/api/index";
import ConTitle from "@/components/ConTitle.vue";

//2.
export default {
  components: { ConTitle },
  name: "gateWay",
  data() {
    return {
      gatewayList: [],
      topTitle: {
        type: "设备", //type如果有内容就会显示上一级
        text: "网关列表",
        msg: "展示******************",
      },
      total: 0, //总条数
      query: {
        currPage: 1, //页码
        pageSize: 5, //当前页有多少条数据
        eui: "",
        online: "", //是否在线的状态  true 在线
        agreement: "all", //组网协议类型
        date: [], //保存的从表单选择的起始和结束日期
      },
      options: [
        //在线状态下拉列表选项
        { value: true, label: "在线" },
        { value: false, label: "离线" },
        { value: "all", label: "全部" },
      ],
    };
  },
  mounted() {
    this.getGatewayList();
  },
  computed: {
    ////保存 日期时间段的字符串
    dateStr() {
      console.log(66);
      let d1 = this.query.date[0];
      let d2 = this.query.date[1];
      //将日期时间转时间戳
      let time1 = d1 && d1.getTime();
      let time2 = d2 && d2.getTime();

      if (time1 && time2) {
        return time1 + "-" + time2;
      } else {
        return "";
      }
    },
  },
  methods: {
    //获取数据的函数
    getGatewayList() {
      //需要将 query里面的date数组更改为 字符串  ***-***
      console.log(this.query);
      var queryStr = { ...this.query };
      if (queryStr != "") {
        //表示有搜索条件
        console.log(queryStr.date, this.dateStr);
        queryStr.date = this.dateStr;
        console.log(queryStr.date, this.dateStr);
      }
      console.log(queryStr);
      $gatewayList(queryStr).then((res) => {
        console.log(res);
        this.gatewayList = res.data.data;
        this.total = res.data.total;
      });
    },
    changeTabFun() {
      // 重新搜索数据
      this.getGatewayList();
    },
    //当页面上显示的数据条数发生变化时会触发这个函数
    handleSizeChange(count) {
      console.log("页面的条数发生变化了", count);
      //将页面显示条数赋值给 pageSize
      this.query.pageSize = count;
      this.query.currPage = 1;
      //重新获取数据
      this.getGatewayList();
    },
    //翻页会触发的函数
    handleCurrentChange(page) {
      console.log("翻页了", page);
      // 修改当前页的页码
      this.query.currPage = page;
      //重新获取数据
      this.getGatewayList();
    },
    //搜索
    searchFun() {
      // 重新搜索数据
      this.getGatewayList();
    },
    //清除条件
    clearFun() {
      //将搜索条件重置
      this.query = {
        currPage: 1, //页码
        pageSize: 5, //当前页有多少条数据
        eui: "",
        online: "", //是否在线的状态  true 在线
        agreement: "all", //组网协议类型
        date: [], //保存的从表单选择的起始和结束日期
      };
      this.getGatewayList();
    },
    //刷新
    refreshFun() {
      this.$router.go(0);
    },
  },
  //过滤器
  filters: {
    onlineType(value) {
      return value ? "在线" : "离线";
    },
    Btime(val) {
      let d = new Date(+val);
      return (
        d.getFullYear() + "年" + (d.getMonth() + 1) + "月" + d.getDate() + "日"
      );
    },
  },
};
</script>

<style lang="scss" scoped>
.gateway-table {
  margin-top: 10px;
}
.box-card {
  margin: 10px;
}
.goPage {
  width: 100%;
  height: 40px;
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 10px;
}
.tab-con {
  margin: 10px;
}
.input-width {
  width: 300px;
}
</style>